
/******************************************************************************************
*******************************************************************************************
**                                                                                       **
**  GVAR Forecasting  (30 July 2009)                                                     **
**                                                                                       **
**                                                                                       **
**  This code was written by L. Vanessa Smith. Please address any questions, comments    **
**  or bugs to: lvs21@cam.ac.uk                                                          **       
**                                                                                       **
**  The code replicates the Diebold and Mariano panel statistics in:         		 **
**                                                                                       **
**    M. Hashem Pesaran, Til Schuermann and L. Vanessa Smith (2009),			 **
**                     									 **  
**   "Forecasting Economic and Financial Variables with Global VARs",                    **
**   International Journal of Forecasting, forthcoming.                    		 **
**                                                                                       **
**                                                                                       **                                               
*******************************************************************************************
******************************************************************************************/


new 2000, 128000;

library pgraph;

format /ld 7,14;

d1=date;


/** Can also use the file xforc_2006Q4 just need to adjust the range accordingly.
The code will filter for the one quarter ahead forecasts ***/

gname1 = "c:\\gauss6.0\\gvar\\ImportFiles\\xforc_1QuarterAhead_2006Q4.xls";   
range1 = "a2:p1609";


dtes={200401,200402,200403,200404,200501,200502,200503,200504,200601,200602,200603,200604};
let string vnam={"Real Output","Inflation","Real Equity","Real Exchange Rate","Short Rate","Long Rate"};


id1_26={110,86,160,158,112,156,193,144,146,142,196,213,223,228,273,293,536,542,548,566,576,578,534,199,456,186}; /** All Countries**/
id1_11={110,86,160,158,112,156,193,144,146,142,196}; /** Industrialised plus China ***/
id1_NoLA ={110,86,160,158,112,156,193,144,146,142,196,536,542,548,566,576,578,534,199,456,186}; /** All Excluding Latin America **/


numbBM=4; /*** Number of Benchmark Models **/

NumbExp=xlsGetSheetCount(gname1);

N=NumbExp-numbBM; /*** Total Number of Models Excluding Benchmarks **/


print"";
print"";
print "**************************************************************************************************************";
print "Select 1, to compute the DM statistic for the GVARAveAve forecast viz a viz that of the AR(1) model";
print "Select 2, to compute the DM statistic for the GVARAveAve forecast viz a viz that of the AR(1) model plus trend";
print "Select 3, to compute the DM statistic for the GVARAveAve forecast viz a viz that of the RW model";
print "Select 4, to compute the DM statistic for the GVARAveAve forecast viz a viz that of the RW model plus drift";
print "Select 5, to compute the DM statistic for the GVARAveAve forecast viz a viz that of the DdPS-GVAR";
print "Select 6, to compute the DM statistic for the GVARAveAve forecast viz a viz that of the DHPS-GVAR";
print "***************************************************************************************************************";


selectvble=con(1,1);
if selectvble==1;
sheetnumb=N+1;
let string ModelBnam={"AR(1) model"};
elseif selectvble==2;
sheetnumb=N+2;
let string ModelBnam={"AR(1) model plus trend"};
elseif selectvble==3;
sheetnumb=N+3;
let string ModelBnam={"RW model"};
elseif selectvble==4;
sheetnumb=N+4;
let string ModelBnam={"RW model plus drift"};
elseif selectvble==5;
sheetnumb=1;
let string ModelBnam={"DdPS-GVAR"};
elseif selectvble==6;
sheetnumb=11;
let string ModelBnam={"DHPS-GVAR"};
endif;

bench=1;

if bench==1;
xforcB = xlsreadm(gname1, range1, sheetnumb, ""); /** N+1: AR1, N+2: AR1 with trend, N+3: RW, N+4: RW with drift; sheet1 is DdPS and sheet 11 is DHPS ***/
endif;

i=1;
do while i<=N;

sheet=i;

xforcA = xlsreadm(gname1, range1, sheet, ""); /*** A: With Equity and Long Rate ***/


e1=(xforcA[.,1] .eq 1);
xforcA_1s=selif(xforcA,e1);
if bench==1;
ee1=(xforcA_1s[.,3] .eq 11) .or (xforcA_1s[.,3] .eq 12) .or (xforcA_1s[.,3] .eq 13)
 .or (xforcA_1s[.,3] .eq 14) .or (xforcA_1s[.,3] .eq 15) .or (xforcA_1s[.,3] .eq 16);
endif;
indx1=indexcat(ee1,1);
xforcA_1s=xforcA_1s[indx1,.];
xforcA_1s_AveWi=meanc((xforcA_1s[.,7:cols(xforcA_1s)])');



if i==1;
ActualData=xforcA_1s[.,6];
codes_xforcA_1s_YDPR=xforcA_1s[.,2:4];
xforcA_1s_AveW=xforcA_1s_AveWi;
elseif i>1;
xforcA_1s_AveW=xforcA_1s_AveW~xforcA_1s_AveWi;
endif;

i=i+1;
endo;

xforcA_1s_AveAve=meanc(xforcA_1s_AveW');
FEabs_xforcA_1s=abs(ActualData-xforcA_1s_AveAve); 
FEsq_xforcA_1s=(ActualData-xforcA_1s_AveAve)^2;


if bench==1;
e2=(xforcB[.,1] .eq 1);
xforcB_1s=selif(xforcB,e2); 
ee2=(xforcB_1s[.,3] .eq 11) .or (xforcB_1s[.,3] .eq 12) .or (xforcB_1s[.,3] .eq 13)
 .or (xforcB_1s[.,3] .eq 14) .or (xforcB_1s[.,3] .eq 15) .or (xforcB_1s[.,3] .eq 16);
indx2=indexcat(ee2,1);
xforcB_1s=xforcB_1s[indx2,.];
FEabs_xforcB_1s=abs(ActualData-xforcB_1s[.,7]); /** We take the first window as AveAve **/
FEsq_xforcB_1s=(ActualData-xforcB_1s[.,7])^2;  /** We take the first window as AveAve **/
endif;

LFabs=codes_xforcA_1s_YDPR~(FEabs_xforcA_1s-FEabs_xforcB_1s);  /** Year code; Variable code; country code ***/
LFsq=codes_xforcA_1s_YDPR~(FEsq_xforcA_1s-FEsq_xforcB_1s);




if bench==1;
vbls={11,12,13,14,15,16};
endif;


MDbarStat={};

i=1;
do while i<=rows(vbls);

LFabsj_i={};
LFsqj_i={};

j=1;
do while j<=rows(dtes);

indx=indexcat(((LFabs[.,1] .eq dtes[j]) .and (LFabs[.,2] .eq vbls[i])),1);
LFabsj=LFabs[indx,.];

indx=indexcat(((LFsq[.,1] .eq dtes[j]) .and (LFsq[.,2] .eq vbls[i])),1);
LFsqj=LFsq[indx,.];

LFabsj_i=LFabsj_i~LFabsj[.,4];
LFsqj_i=LFsqj_i~LFsqj[.,4];

j=j+1;
endo;



Ssq_i_abs=sumc(((LFabsj_i-meanc(LFabsj_i'))^2)')/(cols(LFabsj_i)-1);
Ssq_i_sq=sumc(((LFsqj_i-meanc(LFsqj_i'))^2)')/(cols(LFsqj_i)-1);


MLFabs_i=LFabsj[.,3]~meanc(LFabsj_i');
MLFsq_i=LFsqj[.,3]~meanc(LFsqj_i');

{pos_abs_11}=vecindexcat(MLFabs_i[.,1],id1_11);
{pos_sq_11}=vecindexcat(MLFsq_i[.,1],id1_11);

{pos_abs_NoLA}=vecindexcat(MLFabs_i[.,1],id1_NoLA);
{pos_sq_NoLA}=vecindexcat(MLFsq_i[.,1],id1_NoLA);

{pos_abs_All}=vecindexcat(MLFabs_i[.,1],id1_26);
{pos_sq_All}=vecindexcat(MLFsq_i[.,1],id1_26);

Numer_i_abs_11=meanc(MLFabs_i[pos_abs_11,2]);	
Numer_i_sq_11=meanc(MLFsq_i[pos_sq_11,2]);	

Numer_i_abs_NoLA=meanc(MLFabs_i[pos_abs_NoLA,2]);	
Numer_i_sq_NoLA=meanc(MLFsq_i[pos_sq_NoLA,2]);	

/*** Need this even in the all countries case because for the short-run interest rate the total number
of countries is 25 (no Saudi Arabia) ***/

Numer_i_abs_All=meanc(MLFabs_i[pos_abs_All,2]);	
Numer_i_sq_All=meanc(MLFsq_i[pos_sq_All,2]);	


/****/
Denom_i_abs_11=(1/(cols(LFabsj_i)*rows(pos_abs_11)))*meanc(Ssq_i_abs[pos_abs_11]);
Denom_i_sq_11=(1/(cols(LFsqj_i)*rows(pos_sq_11)))*meanc(Ssq_i_sq[pos_sq_11]);

Denom_i_abs_NoLA=(1/(cols(LFabsj_i)*rows(pos_abs_NoLA)))*meanc(Ssq_i_abs[pos_abs_NoLA]);
Denom_i_sq_NoLA=(1/(cols(LFsqj_i)*rows(pos_sq_NoLA)))*meanc(Ssq_i_sq[pos_sq_NoLA]);

Denom_i_abs_All=(1/(cols(LFabsj_i)*rows(pos_abs_All)))*meanc(Ssq_i_abs[pos_abs_All]);
Denom_i_sq_All=(1/(cols(LFsqj_i)*rows(pos_sq_All)))*meanc(Ssq_i_sq[pos_sq_All]);
/****/

MDbarStat_i_abs_11=Numer_i_abs_11/sqrt(Denom_i_abs_11);
MDbarStat_i_sq_11=Numer_i_sq_11/sqrt(Denom_i_sq_11);

MDbarStat_i_abs_NoLA=Numer_i_abs_NoLA/sqrt(Denom_i_abs_NoLA);
MDbarStat_i_sq_NoLA=Numer_i_sq_NoLA/sqrt(Denom_i_sq_NoLA);

MDbarStat_i_abs_All=Numer_i_abs_All/sqrt(Denom_i_abs_All);
MDbarStat_i_sq_All=Numer_i_sq_All/sqrt(Denom_i_sq_All);


MDbarStat_i=(MDbarStat_i_sq_11~MDbarStat_i_sq_NoLA~MDbarStat_i_sq_All)|
(MDbarStat_i_abs_11~MDbarStat_i_abs_NoLA~MDbarStat_i_abs_All);

MDbarStat=MDbarStat|MDbarStat_i;

i=i+1;
endo;


let string titlgenl={"DM statistic for the GVAR_AveAve forecast viz a viz that of the "}; 
titlgenl=titlgenl $+ ModelBnam;
let string titlGroups={"","","Industrial Plus China", "All Excluding LA", "All Countries"};
let string titLF={"Square Loss", "Absolute Loss"};
titvnam=vec((vnam $~ reshape("",rows(vnam),1))');
titlvnamLF=titvnam$~reshape(titLF,rows(titLF)*rows(vbls),1);

fname ="c:\\gauss6.0\\gvar\\output\\forecast\\DMstatistic_2006Q4.xls";
ret = xlswritesa(titlgenl, fname,"b2",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\forecast\\DMstatistic_2006Q4.xls";
ret = xlswritesa(titlGroups', fname,"b4",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\forecast\\DMstatistic_2006Q4.xls";
ret = xlswritesa(titlvnamLF, fname,"b5",1,"");
fname ="c:\\gauss6.0\\gvar\\output\\forecast\\DMstatistic_2006Q4.xls";
ret = xlsWrite(MDbarStat, fname,"d5",1,"");


d2 = date;
  et = ethsec(d1,d2);
  et = et/100;
  print "Total elapsed time [mins] :" et/60;
  print "";



proc(1)=vecindexcat(y,x);

local j,xiny,mind,xind;

xind=zeros(rows(x),1);

j=1;
do while j<=rows(x);
xiny=indexcat(y,x[j]);
mind=ismiss(xiny);
if mind/=1;
xind[j]=xiny;
endif;
j=j+1;
endo;

if sumc(xind)==0;
xind=miss(0,0);
elseif sumc(xind)/=0;
xind=packr(miss(xind,0));
endif;

retp(xind);
endp;


end;